package com.jy.myhome.controller;

import com.jy.myhome.entity.Users;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jy.myhome.service.UserService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("UserController")
public class UserController extends  UploadController{
    @Autowired
    UserService us;

    //登陆验证
    @ResponseBody
    @RequestMapping("getUser")
    public Users getUser(HttpSession session){
        Users user=(Users) session.getAttribute("user");
        System.out.print(user);
        return user;
    }

    //退出登陆
    @ResponseBody
    @RequestMapping("outLogin")
    public Integer outLogin(HttpSession session){
        session.removeAttribute("user");
        return 1;
    }

    //根据id查询用户信息
    @ResponseBody
    @RequestMapping("findUserOne")
    public Users findUserOne(Integer id){
        return us.findUserOne(id);
    }

    //修改用户信息
    @ResponseBody
    @RequestMapping("updateUser")
    public Integer updateUser(Users user){
        return us.updateUser(user);
    }

    //登陆
    @ResponseBody
    @RequestMapping("login")
    public Map login(Users user, HttpSession session){
        System.out.print(user.getPassword()+"pa");
        //返回实体和状态给前端
        return us.login(user,session);
    }

    //新增(注册)和修改
    @ResponseBody
    @RequestMapping("addAndUpdateUser")
    public Integer addAndUpdateUser(Users user){
        System.out.print(user.getUsername()+"asda");
        System.out.print(user.getPhone()+"asda");
        System.out.print(user.getPassword()+"asda");
        System.out.print(user.getUserid()+"asda");
        if(user.getUserid()!=null){//用户id不为空
            System.out.print("sdfs");
            //修改
            return 0;
        }
        //新增
        return us.register(user);
    }



}
